package com.newcoder.community.controller;

import com.newcoder.community.entity.Page;
import com.newcoder.community.pojo.Comment;
import com.newcoder.community.pojo.DiscussPost;
import com.newcoder.community.pojo.User;
import com.newcoder.community.serverce.CommentService;
import com.newcoder.community.serverce.DiscussPostService;
import com.newcoder.community.serverce.LikeService;
import com.newcoder.community.serverce.UserService;
import com.newcoder.community.utill.CommunityConstant;
import com.newcoder.community.utill.CommunityUtill;
import com.newcoder.community.utill.HostHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Controller
@RequestMapping("/discuss")
public class DiscussPostController implements CommunityConstant {
    @Autowired
    private HostHolder hostHolder;

    @Autowired
    private DiscussPostService discussPostService;
    @Autowired
    private UserService userService;
    @Autowired
    private CommentService commentService;
    @Autowired
    private LikeService likeService;

    @PostMapping("/add")
    @ResponseBody
    public String addDiscussPost(String title, String content) {
        User user = hostHolder.getUser();
        if (user == null) {
            return CommunityUtill.getJson(403, "你还没有登录");
        }
        DiscussPost discussPost = new DiscussPost();
        discussPost.setUserId(user.getId());
        discussPost.setTitle(title);
        discussPost.setContent(content);
        discussPost.setCreateTime(new Date());
        discussPostService.addDiscussPost(discussPost);
        System.out.println("增加成功");
        return CommunityUtill.getJson(0, "消息发布成功");

    }

    @GetMapping("/detail/{id}")
    public String toPostDetail(@PathVariable("id") int id, Model model, Page page) {
        DiscussPost discussPost = discussPostService.queryById(id);
        model.addAttribute("post", discussPost);
        User user = userService.selectById(discussPost.getUserId());
        model.addAttribute("user", user);
        //点赞数量
        long likeCount = likeService.findEntityLikeCount(ENTITY_TYPE_POST, id);
        model.addAttribute("likeCount", likeCount);
        //点赞状态
        int likeStatus = hostHolder.getUser() == null ? 0 : likeService.findEntityLikeStatus(hostHolder.getUser().getId(), ENTITY_TYPE_POST, id);
        model.addAttribute("likeStatus", likeStatus);
        page.setRows(discussPost.getCommentCount());
        page.setPath("/discuss/detail/" + id);
        page.setLimit(5);
        List<Comment> comments = commentService.selectCommentByEntity(ENTITY_TYPE_POST, discussPost.getId(), page.getOffset(), page.getLimit());
        List<Map<String, Object>> commentVoList = new ArrayList<>();
        if (comments != null) {
            for (Comment comment : comments) {
                Map<String, Object> postMap = new HashMap<>();
                postMap.put("comment", comment);
                postMap.put("user", userService.selectById(comment.getUserId()));
                likeCount = likeService.findEntityLikeCount(ENTITY_TYPE_COMMENT, comment.getId());
                postMap.put("likeCount", likeCount);
                //点赞状态
                likeStatus = hostHolder.getUser() == null ? 0 : likeService.findEntityLikeStatus(hostHolder.getUser().getId(), ENTITY_TYPE_COMMENT, comment.getId());
                postMap.put("likeStatus", likeStatus);
                List<Comment> replyList = commentService.selectCommentByEntity(ENTITY_TYPE_COMMENT, comment.getId(), 0, Integer.MAX_VALUE);
                List<Map<String, Object>> replyVoList = new ArrayList<>();
                if (replyList != null) {
                    for (Comment reply : replyList) {
                        Map<String, Object> replyMap = new HashMap<>();
                        replyMap.put("reply", reply);
                        replyMap.put("user", userService.selectById(reply.getUserId()));
                        User target = reply.getTargetId() == 0 ? null : userService.selectById(reply.getTargetId());
                        likeCount = likeService.findEntityLikeCount(ENTITY_TYPE_COMMENT, reply.getId());
                        replyMap.put("likeCount", likeCount);
                        //点赞状态
                        likeStatus = hostHolder.getUser() == null ? 0 : likeService.findEntityLikeStatus(hostHolder.getUser().getId(), ENTITY_TYPE_COMMENT, reply.getId());
                        replyMap.put("likeStatus", likeStatus);
                        replyMap.put("target", target);
                        replyVoList.add(replyMap);
                    }
                }
                postMap.put("replys", replyVoList);
                int i = commentService.selectCountByEntity(ENTITY_TYPE_COMMENT, comment.getId());
                postMap.put("replyCount", i);
                commentVoList.add(postMap);
            }

        }
        model.addAttribute("comments", commentVoList);
        return "/site/discuss-detail.html";

    }


}
